//
//  SE_Select_VC.m
//  SkyEye
//
//  Created by dllo on 16/8/9.
//  Copyright © 2016年 SE. All rights reserved.
//

#import "SE_Select_VC.h"
#import "UIViewController+RESideMenu.h"
#import "SENetTool.h"
#import "SE_Header_Model.h"
#import "SE_Item_Model.h"
#import "SE_Select_View.h"
#import <MJRefresh.h>

@interface SE_Select_VC ()

@property (nonatomic, retain) NSString *nextPageUrl;
@property (nonatomic, retain) NSMutableArray *headerArray;
@property (nonatomic, retain) SE_Select_View *selectView;

@end

@implementation SE_Select_VC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"精选";
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[[UIImage imageNamed:@"left.jpg"] imageWithRenderingMode:1] style:UIBarButtonItemStylePlain target:self action:@selector(presentLeftMenuViewController:)];

    NSString *urlStr = @"http://baobab.wandoujia.com/api/v2/feed?num=2&udid=7a5fe163c0054cb896bacda33ceeaa4d0e0ea60b&vc=109&vn=2.1.1&deviceModel=HTC%20T329t&first_channel=eyepetizer_sogou_market&last_channel=eyepetizer_sogou_market";
    
    self.headerArray = [NSMutableArray array];
    [self handleData:urlStr];
    [self createSelectView];
    
//    self.selectView.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
//        [self.headerArray removeAllObjects];
//        [self.selectView.tableView reloadData];
//        [self handleData:urlStr];
//    }];
//    self.selectView.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
//        [self handleData:self.nextPageUrl];
//    }];
}

-(void)handleData:(NSString *)urlStr{
    
    [SENetTool GET:urlStr body:nil headerFile:nil response:SEJSON success:^(id result) {
        self.nextPageUrl = [result objectForKey:@"nextPageUrl"];
        NSArray *issueList = [result objectForKey:@"issueList"];
        for (NSDictionary *issueListDic in issueList) {
            SE_Header_Model *headerModel = [[SE_Header_Model alloc]init];
            NSArray *itemList = [issueListDic objectForKey:@"itemList"];
            NSMutableArray *itemListArray = [NSMutableArray array];
            for (NSDictionary *itemListDic in itemList) {
                NSString *type = [itemListDic objectForKey:@"type"];
#pragma mark - textHeader
                if ([type isEqualToString:@"textHeader"]) {
                    NSDictionary *dataDic = [itemListDic objectForKey:@"data"];
                    headerModel.text = [dataDic objectForKey:@"text"];
                }
#pragma mark - vidoe
                if ([type isEqualToString:@"video"]) {
                    NSDictionary *dataDic = [itemListDic objectForKey:@"data"];
                    SE_Item_Model *model = [[SE_Item_Model alloc]initWithDic:dataDic];
                    NSDictionary *cover = [dataDic objectForKey:@"cover"];
                    model.feed = [cover objectForKey:@"feed"];
                    model.blurred = [cover objectForKey:@"blurred"];
                    [itemListArray addObject:model];
                }
            }
            headerModel.itemModelArray = itemListArray;
            [self.headerArray addObject:headerModel];
        }
        
        
        
        self.selectView.headerArray = self.headerArray;
//        [self.selectView.tableView.mj_header endRefreshing];
//        [self.selectView.tableView.mj_footer endRefreshing];
        
        
        
    } failure:^(NSError *error) {
        NSLog(@"%@", error);
    }];
}

-(void)createSelectView{
    self.selectView = [[SE_Select_View alloc]initWithFrame:self.view.frame];
    [self.view addSubview:self.selectView];
}

@end

















